package com.tvserials.notification.services;

import com.tvserials.notification.dto.Episode;
import com.tvserials.notification.dto.Serial;
import com.tvserials.notification.vo.SourceType;
import com.tvserials.notification.vo.TvEpisodeFilterVO;
import org.springframework.http.*;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Created by Alex on 01.05.2014.
 */
public class RemoteNotificationService implements NotificationService {
    public static final String BASE_URL = "http://10.0.3.2:8080/n";
    private static RestTemplate restTemplate;

    @Override
    public String userSignIn(String username, String password) {
        return null;
    }

    @Override
    public List<Episode> getEpisodes(TvEpisodeFilterVO filterVO) {
        // The URL for making the GET request
        final String url = BASE_URL + "/episodes";
        //This is where we get the RestTemplate and add the message converters
        RestTemplate restTemplate = getRestTemplate();
        HttpEntity<TvEpisodeFilterVO> requestEntity = new HttpEntity<TvEpisodeFilterVO>(filterVO, getRequestHeader());
        ResponseEntity<Episode[]> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, Episode[].class);
        Episode[] body = responseEntity.getBody();
        if (body != null && body.length > 0) {
            return new ArrayList(Arrays.asList(body));
        }
        return new ArrayList<Episode>();
    }

    @Override
    public List<Serial> getSerials(SourceType sourceType) {
        final String url = BASE_URL + "/serials";
        //This is where we get the RestTemplate and add the message converters
        RestTemplate restTemplate = getRestTemplate();
        HttpEntity<SourceType> requestEntity = new HttpEntity<SourceType>(sourceType, getRequestHeader());
        ResponseEntity<Serial[]> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, Serial[].class);
        Serial[] body = responseEntity.getBody();
        if (body != null && body.length > 0) {
            return new ArrayList(Arrays.asList(body));
        }
        return new ArrayList<Serial>();
    }

    private HttpHeaders getRequestHeader() {
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
        return requestHeaders;
    }

    private RestTemplate getRestTemplate() {
        if (restTemplate == null) {
            restTemplate = new RestTemplate();
            restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
            restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
        }
        return restTemplate;
    }
}
